<template>
  <div>
    <el-dialog
      title="新增账户"
      :visible.sync="addDialog"
      width="30%"
      :before-close="resetForm"
    >
      <el-form
        :model="addForm"
        :rules="rules"
        label-width="80px"
        ref="ruleForm"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="addForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input
            v-model="addForm.nickName"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="号码" prop="phonenumber">
          <el-input
            v-model="addForm.phonenumber"
            placeholder="请输入正确的手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="addForm.email"
            placeholder="请输入正确的邮箱地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="addForm.password"
            placeholder="请输入正确的密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >确定</el-button
          >
          <el-button @click="resetForm">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    addDialog: {
      type: Boolean,
      default: () => false,
    },
  },
  data() {
    return {
      addForm: {
        userName: "",
        nickName: "",
        phonenumber: "",
        email: "",
        password: "",
        platform: "Manager",
        status: 0,
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        nickName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        phonenumber: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        email: [
          {
            pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            required: true,
            message: "请输入正确的邮箱地址",
            trigger: "blur",
          },
        ],
        password: [
          {
            pattern: /^(?=.*[a-z])(?=.*\d)[a-z\d]{5,10}$/,
            required: true,
            message: "密码应包含小写字母和数字并且长度在 5 到 10 之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    submitForm(formName) {
      console.log(this.addForm);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("handleAddUser", this.addForm);
        }
      });
    },
    resetForm() {
      if (!this.addDialog) return;
      this.$emit("closeAdddia");
      this.addForm = {
        userName: "",
        nickName: "",
        phonenumber: "",
        email: "",
        password: "",
        platform: "Manager",
        status: 0,
      };
      this.$refs.ruleForm.resetFields();
    },
  },
  watch: {
    addDialog(newVal) {
      if (!newVal) {
        this.addForm = {
          userName: "",
          nickName: "",
          phonenumber: "",
          email: "",
          password: "",
          platform: "Manager",
          status: 0,
        };
      }
    },
  },
};
</script>
